package gov.goias.recad.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class FiltroLogin implements  Filter {
	
	@Override
	public void init(final FilterConfig configuracao) throws ServletException {
		
	}

	@Override
	public void doFilter(final ServletRequest  requisicao, 
			             final ServletResponse resposta,
			             final FilterChain     cadeia) throws IOException, 
			                                              ServletException {
		
		final HttpServletRequest  requisicaoHttp = (HttpServletRequest)  requisicao;  
		final HttpServletResponse respostaHttp   = (HttpServletResponse) resposta;
		
		final HttpSession sessao = requisicaoHttp.getSession(false);
		
		if(sessao == null || sessao.getAttribute("servidorLogado") == null){
			respostaHttp.sendRedirect(requisicaoHttp.getContextPath() + "/login.xhtml");
		}else{
			cadeia.doFilter(requisicao, resposta);     
		}       
	}

	@Override
	public void destroy() {
		
	}     
	
}